CS211 Lab Policy:
Instructions:
You will create three (3) MATLAB program file named Lab7a.m, Lab7b.m, and Lab7c.m for this lab. For each step below, add appropriate MATLAB code and label each step with appropriate comments. Make sure you test your code for each step before proceeding to the next step.
For Lab7a.m: Write code that implements several simple loops.
Add code that repeatedly displays a random integer from 1 to
6 (the roll of a die) until the first number displayed is redisplayed.
Thus, numbers after the first number may be repeated any number of times,
but the first number will be repeated exactly once. Hint: use the
following expression to get a random integer from 1 to 6:
floor(rand()*6) + 1
For Lab7b.m: Write code that plays the game of "hi low" with the user. Your code should do the following:
For Lab7c.m: Add code to a function that finds roots of an equation using the Newton-Raphson method (see description in the lesson notes).
Start with the file Lab7c.m (right-click and select "Save Target As...")
Modify the code to display the new "guess" value on each
iteration of the loop (i.e., display the value of X). Display the value with
at least 15 digits of accuracy after the decimal point. (This will require
the use of the fprintf() function.)
Add a counting variable that counts the number of times the
loop executes and display this value after the loop terminates.
The equation in the function f(),
Y = 3X2 - 12.1X + 5, has two
roots, 0.467383678385362 and 3.565949654947949. Make sure you can execute
the program and enter appropriate initial guess values to find each one. (No
code modification required.)
Modify the equations in the functions f() and f_derivative() to Y = 3X2 - 4X + 12 and Y = 6X - 4 respectively and then try to find the roots of this new equation. What happens? Can you find the roots? (Remember, type CTRL-C to terminate an infinite loop.)
Turn-in:
Submit your lab7a.m , lab7b.m and lab7c.m files.